import serial
import serial.tools.list_ports
from typing import List
from ..models import SerialInfo


def list_serial_ports() -> List[SerialInfo]:
    # 获取当前系统所有串口设备信息
    ports = serial.tools.list_ports.comports()
    serial_info_list: List[SerialInfo] = []
    # 遍历所有串口设备
    for port, desc, hwid in sorted(ports):
        # 使用字典的方式初始化 SerialInfo
        serial_info: SerialInfo = {
            'port': port,
            'description': desc,
            'hwid': hwid
        }
        serial_info_list.append(serial_info)

    return serial_info_list


if __name__ == '__main__':
    list_serial_ports()
